package com.demo.demo.serviceImpl;

import com.demo.demo.entity.Class;
import com.demo.demo.entity.ClassExample;
import com.demo.demo.entity.ClassExample.Criteria;
import com.demo.demo.mapper.ClassMapper;
import com.demo.demo.service.ClassService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ClassServicesImpl implements ClassService {
	final Logger logger = LoggerFactory.getLogger(ClassServicesImpl.class);
	@Autowired
	private ClassMapper classMapper;
	@Override
	public int deleteClassById(String id) throws Exception {
		return classMapper.deleteByPrimaryKey(id);
	}

	@Override
	@CacheEvict(value = "findClassById" ,allEntries = true)
	public int updateClass(Class class1) throws Exception {
		return classMapper.updateByPrimaryKeySelective(class1);
	}

	@Override
	@Cacheable(value = "findClassById")
	public Class selectClassById(String id) throws Exception {
		Class aClass = classMapper.selectByPrimaryKey(id);
		logger.info("findClassById");
		return aClass;
	}

	@Override
	public int addClass(Class class1) throws Exception {
		return classMapper.insert(class1);
	}

	@Override
	public List<Class> selectAllClass(Class classInfo) throws Exception {
		ClassExample classExample = new ClassExample();
		Criteria createCriteria = classExample.createCriteria();
		if(classInfo.getId() != null && !"".equals(classInfo.getId())) {
			createCriteria.andIdLike("%"+classInfo.getId()+"%");
		}
		if(classInfo.getClassname() != null && !"".equals(classInfo.getClassname())) {
			createCriteria.andClassnameLike("%"+classInfo.getClassname()+"%");
		}
		return classMapper.selectByExample(classExample);
	}
	@Override
	public List<Class> selectAllClassDistinct() throws Exception {
		ClassExample classExample = new ClassExample();
		
		classExample.setDistinct(true);
		return classMapper.selectByExample(classExample);
	}

}
